package com.ynet.middleground.contract.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.timevale.esign.sdk.tech.v3.client.ServiceClient;
import com.timevale.esign.sdk.tech.v3.client.ServiceClientManager;
import com.ynet.core.exception.BusinessException;
import com.ynet.file.service.FileService;
import com.ynet.file.starter.FileServiceConfiguration;
import com.ynet.middleground.contract.constant.SystemConsts;
import com.ynet.middleground.contract.external.esign.EsignConfig;

/**
 * @author liulx
 * @description 通用工具类
 * @date 2020-04-21 22:41
 */
@Component
public class CommUtils {

    @Autowired
    FileServiceConfiguration fileServiceConfiguration;

    @Autowired
    FileService fileService;

    @Autowired
    EsignConfig esignConfig;

    /**
     * 生成标准 rediskey <br />
     * 规则: 公司框架名称:表前缀:字段名:唯一主键 <br/>
     * eg: ifp:uc:channel:channellist
     *
     * @param tablesName 表名
     * @param fieldName 字段名
     * @param id 唯一主键
     * @return redis key
     */
    public static String getRedisKey(String tablesName, String fieldName, String id) {
        // 公司框架名称:表前缀:字段名:唯一主键 ifp:uc:channel:channellist
        return SystemConsts.FRAMEWORK_NAME + ":" + tablesName + ":" + fieldName + ":" + id;
    }

    /**
     * 根据文件短路径获得文件完整地址
     *
     * @param shortOssUrl 文件短路径地址
     *            {@link FileService#uploadFile(java.lang.String, java.io.InputStream, java.lang.String, java.lang.String)}
     *            eg: zhogntai/test.pdf
     * @return oss 完整路径 eg: http://oss-cn-qingdao-qdg-d01-a.cloud.qdport.com/zhongtai/test.pdf
     */
    public String getOssFullUrl(String shortOssUrl) {
        if (StringUtils.isBlank(shortOssUrl)) {
            throw new BusinessException("短路径地址转长路径地址时，短路径地址不能为空", "ECCT0001");
        }
        String endpoint = fileServiceConfiguration.getConfigs().get(0).getOssConfig().getEndpoint();
        return endpoint.substring(0, endpoint.lastIndexOf("/")) + "/" + shortOssUrl;
    }

    /**
     * 获取 oss bucketName
     *
     * @return bucketName
     */
    public String getOssBucketName() {
        OSSClient ossClient = (OSSClient)fileService.getOriginClient("oss");
        String path = ossClient.getEndpoint().getPath();
        return path.substring(path.indexOf("/") + 1);
    }

    /**
     * 获取 OSS 域名
     *
     * @return oss 域名地址
     */
    public String getOssHost() {
        OSSClient ossClient = (OSSClient)fileService.getOriginClient("oss");
        return ossClient.getEndpoint().getHost();
    }

    /**
     * 文件路径地址转换，获取文件完整路径
     *
     * <pre>
     * 如果文件地址包含 OSS 域名，则认为是全路径，返回入参地址；
     * 否则认为文件是短路径，则将入参地址转换成全路径地址
     * </pre>
     *
     * @param path 文件地址
     * @return 文件完整路径
     */
    public String transferFileUrl(String path) {
        return path.contains(getOssHost()) ? path : getOssFullUrl(path);
    }

    /**
     * 获取 esign 服务客户端
     *
     * @return esign 服务客户端 {@link ServiceClient}
     */
    public ServiceClient getServiceClient() {
        return ServiceClientManager.get(esignConfig.getProjectId());
    }

}
